<?php


namespace app\index\business;


use app\common\model\ConsignorReplenish;
use app\common\model\LogisticsReplenish;
use app\common\model\MemberInfo;
use app\common\model\MemberInfoAddUser;
use think\Exception;

class enterpriseBus
{
    public $memberInfoMode = null;
    public $consignorReplenish= null;
    public $logisticsReplenish=null;
    public $memberInfoAddUser = null;
    public function __construct()
    {
        $this->memberInfoMode = new MemberInfo();
        $this->consignorReplenish = new ConsignorReplenish();
        $this->logisticsReplenish = new LogisticsReplenish();
        $this->memberInfoAddUser = new MemberInfoAddUser();
    }
    public function getMemberPageWhereBus($where,$paging,$title,$address,$trade_name){
//        dump($where);exit();
        if(array_key_exists('member_category',$where)){
            $res = $this->memberInfoMode->getMemberPageBySupplementWhere($where,$paging,$title,$address,$trade_name);
        }else{
            $res = $this->memberInfoMode->getMemberPageWhere($where,$paging,$title,$address);
        }

       return $res;
    }
    public function getDetailBus($id,$member_id){

        $visitInfo = $this->memberInfoMode->getMemberInfoById(['member_id'=>$member_id]);
        if(empty($visitInfo)){
            $res = $this->memberInfoMode->getDetailFieldsById($id);
            if(empty($res)){
                throw new Exception('数据库连接错误');
            }
            $supplement = null;
        }else{
            if($visitInfo->member_grade==2){
                $res = $this->memberInfoMode->getDetailById($id);
                if(empty($res)){
                    throw new Exception('数据库连接错误');
                }
                $member_id = [
                    'member_id' => $res->member_id
                ];
                $supplement=null;
                if($res->member_category==1){
                    $supplement = $this->consignorReplenish->getReplenishInfoByWhere($member_id);
                }
                if($res->member_category==2){
                    $supplement = $this->logisticsReplenish->getReplenishInfoByWhere($member_id);
                }
            }else{
                $res = $this->memberInfoMode->getDetailFieldsById($id);
                if(empty($res)){
                    throw new Exception('数据库连接错误');
                }
                $supplement = null;
            }

        }

        $result = [
            "memberInfo"=>$res,
            "supplement"=>$supplement
        ];
        return $result;
    }
    public function pushFacilitatorBus($member_id,$member_info_id,$member_info_name){
        $whereListAll = [
            'member_info_id'=>$member_info_id
        ];
        $resListAll = $this->memberInfoAddUser->getListAll($whereListAll);

        $memberInfoWhere = [
            'member_id'=>$member_id,
            'status' => 1
        ];
        $resultMemberInfo = $this->memberInfoMode->getMemberInfoById($memberInfoWhere);
        if(empty($resultMemberInfo)){
            throw new Exception('申请的企业信息不存在');
        }
        $postData = [
            'member_id'=>$member_id,
            'member_info_id'=>$member_info_id,
            'company_name'=>$resultMemberInfo['name'],
            'member_info_name'=>$member_info_name
        ];
        if(empty($resListAll)){
            $res = $this->memberInfoAddUser->addMemberInfo($postData);
        }else{
            $isPush = false;
            foreach ($resListAll as $item){
                if($item['member_id'] == $member_id){
                    $isPush = true;
                    break;
                }
            }
            if($isPush){
                throw new Exception('请不要重复申请');
            }else{
                $res = $this->memberInfoAddUser->addMemberInfo($postData);
            }
        }
        return $res;
    }

    public function isApplyAddBus($member_id,$member_info_id){
        $whereListAll = [
            'member_info_id'=>$member_info_id
        ];
        $resListAll = $this->memberInfoAddUser->getListAll($whereListAll);
        $isPush = false;
        foreach ($resListAll as $item){
            if($item['member_id'] == $member_id){
                $isPush = true;
                break;
            }
        }
        return $isPush;
    }
}